<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="keyword" class="w-[310px]">
          <a-input v-model:value="formState.keyword" placeholder="单号/供应商名称" />
        </a-form-item>
        <a-form-item name="dateRange" label="确认日期" class="ml-3 mr-3">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="flex mb-2">
        <BaseExport
          type="药库"
          name="采购汇总"
          code="000798"
          :search-params="{ ...formState, startTime: formState.dateRange?.[0], endTime: formState.dateRange?.[1] }"
          >导出</BaseExport
        >
        <a-button type="primary" @click="onPrint">打印</a-button>
      </a-space>
      <BaseTable :loading="loading" column-code="000798" :data-source="tableData" bordered fill :extra-height="60">
        <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'xh'">
            <div>{{ index + 1 }}</div>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>合计</a-table-summary-cell>
              <a-table-summary-cell>-</a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.je) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total?.fps }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.cgje) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total?.cgfps }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.thje) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total?.thfps }}</div>
                </a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import { formatAmount } from '@/utils/format.js'
import { post } from '@/utils/request'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().subtract(1, 'month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const total = ref({})

const { loading, tableData, getData } = useTable({
  url: '/drugStorage/psiform/getDrugPurchaseSummary',
  immediate: true,
  isPage: false,
  formatParams: () => {
    return {
      ...formState.value,
      startTime: formState.value.dateRange?.[0],
      endTime: formState.value.dateRange?.[1]
    }
  },
  formatResult: (result) => {
    total.value = result.reduce(
      (sum, current) => {
        return {
          je: sum.je + (current.je ? Number(current.je) : 0),
          fps: sum.fps + (current.fps ? Number(current.fps) : 0),
          cgje: sum.cgje + (current.cgje ? Number(current.cgje) : 0),
          cgfps: sum.cgfps + (current.cgfps ? Number(current.cgfps) : 0),
          thje: sum.thje + (current.thje ? Number(current.thje) : 0),
          thfps: sum.thfps + (current.thfps ? Number(current.thfps) : 0)
        }
      },
      { je: 0, fps: 0, cgje: 0, cgfps: 0, thje: 0, thfps: 0 }
    )
    return result
  }
})

// 打印
const onPrint = async () => {
  const { code, msg, data } = await post('/drugStorage/psiform/printDrugPurchaseSummary', {
    ...formState.value,
    startTime: formState.value.dateRange?.[0],
    endTime: formState.value.dateRange?.[1]
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
</script>
<style lang="less" scoped></style>
